<script setup lang="ts" name="Notice">
import editPage from "./edit.vue";
import detailPage from "./detail.vue";
import { delNoticeApi, listNoticeApi } from "@/api/system/notice";

const { proxy } = getCurrentInstance();

const { sys_notice_type, sys_notice_status } = useDict("sys_notice_type", "sys_notice_status");

// 遮罩层
const loading = ref(false);

// 搜索栏元素
const queryNoticeRef = ref(null);

// 表单Id
const id = ref(null);

// 选中数组
const selection = ref<NoticeRecord[]>([]);

// 非单个禁用
const single = ref(true);

// 非多个禁用
const multiple = ref(true);

// 通知公告表数据
const noticeList = ref<NoticeRecord[]>([]);

// 弹出层标题
const title = ref("");

// 编辑弹出层
const openEdit = ref(false);

// 详情弹出层
const openDetail = ref(false);

//总条数
const total = ref(0);

// 列信息
const columns = ref<TableColumnsRecord>({
  colNo: { label: "序号", visible: true },
  noticeId: { label: "公告ID", visible: true },
  noticeTitle: { label: "公告标题", visible: true },
  noticeType: { label: "公告类型", visible: true },
  status: { label: "状态", visible: true },
  noticeContent: { label: "内容", visible: true },
  createBy: { label: "创建者", visible: true },
  createTime: { label: "创建时间", visible: true },
});

// 查询参数
const queryParams = ref<NoticeRecord>({
  pageNum: 1,
  pageSize: 10,
  noticeId: null,
  noticeTitle: null,
  noticeType: null,
  status: null,
  noticeContent: null,
  createBy: null,
  createTime: null,
});

/** 查询通知公告列表 */
function getList() {
  loading.value = true;
  listNoticeApi(queryParams.value).then(response => {
    noticeList.value = response.rows;
    total.value = response.total;
    loading.value = false;
    noticeList.value.forEach((item, index) => {
      item['colNo'] = sequenceNo(index, queryParams.value);
    });
  });
}

/** 关闭弹窗 */
function closeDialog(refresh: boolean) {
  if (refresh) {
    getList();
  }
  openEdit.value = false;
  openDetail.value = false;
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryNoticeRef.value.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selectionList: NoticeRecord[]) {
  selection.value = selectionList;
  single.value = selectionList.length !== 1;
  multiple.value = !selectionList.length;
}

/** 新增按钮操作 */
function handleAdd() {
  id.value = null;
  openEdit.value = true;
  title.value = "新增通知公告";
}

/** 修改按钮操作 */
function handleUpdate(row: NoticeRecord) {
  id.value = row.noticeId || selection.value.map(item => item.noticeId);
  openEdit.value = true;
  title.value = "修改通知公告";
}

/** 详情按钮操作 */
function handleDetails(row: NoticeRecord) {
  id.value = row.noticeId;
  openDetail.value = true;
  title.value = "通知公告详情";
}

/** 删除按钮操作 */
function handleDelete(row: NoticeRecord | any) {
  let ids = [];
  let noticeTitles = [];
  if (row.noticeId) {
    ids = [row.noticeId];
    noticeTitles = [row.noticeTitle];
  } else {
    selection.value.forEach(item => {
      ids.push(item.noticeId);
      noticeTitles.push(item.noticeTitle);
    })
  }

  // @ts-ignore
  proxy.$modal.confirm('是否确认删除公告标题为"' + noticeTitles + '"的数据项?', "系统提示", {
    type: "warning",
  }).then(() => {
    return delNoticeApi(ids);
  }).then(() => {
    getList();
    // @ts-ignore
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

getList();
</script>

<template>
  <div class="app-container">
    <div class="back-item">
      <el-form :model="queryParams" ref="queryNoticeRef" :inline="true" label-width="68px">
        <el-form-item label="公告标题" prop="noticeTitle">
          <el-input
              v-model="queryParams.noticeTitle"
              placeholder="请输入公告标题"
              clearable
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="公告类型" prop="noticeType">
          <el-select
              v-model="queryParams.noticeType"
              placeholder="请选择公告类型"
              clearable
              @keyup.enter.native="handleQuery"
          >
            <el-option
                v-for="(item, index) in sys_notice_type"
                :key="index"
                :label="item.label"
                :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select
              v-model="queryParams.status"
              placeholder="请选择状态"
              clearable
              @keyup.enter.native="handleQuery"
          >
            <el-option
                v-for="(item, index) in sys_notice_status"
                :key="index"
                :label="item.label"
                :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="操作人员" prop="createBy">
          <el-input
              v-model="queryParams.createBy"
              placeholder="请输入操作人员"
              clearable
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item class="item-search">
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button type="warning" icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="back-item">
      <div class="table-top mb8">
        <div class="title title-left">
          通知公告列表
        </div>
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-has-permi="['system:notice:add']"
        >新增
        </el-button>
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-has-permi="['system:notice:remove']"
        >删除
        </el-button>
        <span class="table-top-line" />
        <right-toolbar @queryTable="getList" :columns="columns" />
      </div>
      <el-table
          v-loading="loading"
          :data="noticeList"
          stripe
          v-tablesticky
          @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" align="center" width="55" prop="colNo" v-if="columns.colNo.visible" />
        <el-table-column label="公告标题" prop="noticeTitle" align="center" min-width="160" show-overflow-tooltip v-if="columns.noticeTitle.visible" />
        <el-table-column label="公告类型" prop="noticeType" align="center" v-if="columns.noticeType.visible">
          <template #default="scope">
            <dict-tag :options="sys_notice_type" :value="scope.row.noticeType" />
          </template>
        </el-table-column>
        <el-table-column label="状态" prop="status" align="center" v-if="columns.status.visible">
          <template #default="scope">
            <dict-tag :options="sys_notice_status" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="创建者" prop="createBy" align="center" v-if="columns.createBy.visible" />
        <el-table-column label="创建时间" prop="createTime" align="center" min-width="160" v-if="columns.createTime.visible" />
        <el-table-column label="操作" align="center" fixed="right" min-width="120">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button
                  link
                  type="primary"
                  icon="Edit"
                  @click="handleUpdate(scope.row)"
                  v-has-permi="['system:notice:edit']"
              />
            </el-tooltip>
            <el-tooltip content="详情" placement="top">
              <el-button
                  link
                  type="primary"
                  icon="Document"
                  @click="handleDetails(scope.row)"
                  v-has-permi="['system:notice:query']"
              />
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button
                  link
                  type="primary"
                  icon="Delete"
                  @click="handleDelete(scope.row)"
                  class="text-danger"
                  v-has-permi="['system:notice:remove']"
              />
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <pagination
          :total="total"
          open-fixed
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
      />
      <div class="line-magin" />
    </div>

    <!-- 添加或修改通知公告对话框 -->
    <edit-page v-if="openEdit" :id="id" :title="title" @closeDialog="closeDialog" />
    <detail-page v-if="openDetail" :id="id" :title="title" @closeDialog="closeDialog" />
  </div>
</template>

<style lang="scss" scoped>

</style>